package com.hzya.frame.system.login.service.impl;

import com.hzya.frame.system.login.service.ILoginService;
import com.hzya.frame.system.module.dao.IModuleDao;
import com.hzya.frame.system.module.entity.ModuleEntity;
import com.hzya.frame.system.role.dao.IRoleDao;
import com.hzya.frame.system.role.entity.RoleEntity;
import com.hzya.frame.system.user.dao.IUserDao;
import com.hzya.frame.system.user.entity.UserEntity;
import com.hzya.frame.utils.core.entity.JsonResultEntity;
import com.hzya.frame.utils.help.AES;
import com.hzya.frame.utils.help.JsonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 
 * @author 郭军辉
 *
 *         2016年12月14日下午5:41:45
 */
@Service
public class ILoginServiceImpl implements ILoginService {
	/** 用户dao */
	@Autowired
	private IUserDao userDao;

	/** 模块dao */
	@Autowired
	private IModuleDao moduleDao;
	/** 角色DAO */
	@Autowired
	private IRoleDao roleDao;

	@Override
	public boolean login(UserEntity bean) throws Exception {
		JsonResultEntity resultDomain = null;
		if (!checkUserName(bean)) {
			resultDomain = new JsonResultEntity("用户名不存在!", "YHMBCZ", false);
			throw new RuntimeException(JsonUtils.bean2Json(resultDomain), null);
		}
		if (!checkPassWord(bean)) {
			resultDomain = new JsonResultEntity("密码错误!", "MMCW", false);
			throw new RuntimeException(JsonUtils.bean2Json(resultDomain));
		}
		try {
			bean = getFullUserInfo(bean);
		} catch (Exception e) {
			resultDomain = new JsonResultEntity(e.getMessage(), "OTHER", false);
			throw new RuntimeException(JsonUtils.bean2Json(resultDomain));
		}

		return true;
	}

	@Override
	public boolean checkUserName(UserEntity bean) throws Exception {
		UserEntity paraDomain = new UserEntity();
		paraDomain.setLogin_name(bean.getLogin_name());
		//paraDomain.setPlatform_account(bean.getPlatform_account());
		paraDomain = userDao.queryOne(paraDomain);
		if (null != paraDomain) {
			BeanUtils.copyProperties(paraDomain, bean, "password");
			return true;
		}
		return false;
	}

	public boolean checkCanUse(UserEntity bean) throws Exception {
		UserEntity paraDomain = new UserEntity();
		paraDomain.setLogin_name(bean.getLogin_name());
		//paraDomain.setPlatform_account(bean.getPlatform_account());
		paraDomain = userDao.queryOne(paraDomain);
		/*
		 * if (null != paraDomain && "Y".equals(bean.getCan_use())) { return
		 * true; }
		 */
		return false;
	}

	/**
	 * 检查平台帐号
	 * 
	 * @return
	 * @throws Exception
	 */
	public boolean checkPlatformAccount(UserEntity bean) throws Exception {
		UserEntity paraDomain = new UserEntity();
		paraDomain.setLogin_name(bean.getLogin_name());
		//paraDomain.setPlatform_account(bean.getPlatform_account());
		paraDomain = userDao.queryOne(paraDomain);
		if (null != paraDomain) {
			BeanUtils.copyProperties(paraDomain, bean);
			return true;
		}
		return false;
	}

	@Override
	public boolean checkPassWord(UserEntity bean) throws Exception {
		UserEntity paraDomain = new UserEntity();
		paraDomain.setPassword(AES.Encrypt(bean.getPassword()));
		paraDomain.setLogin_name(bean.getLogin_name());
		//paraDomain.setPlatform_account(bean.getPlatform_account());
		paraDomain = userDao.queryOne(paraDomain);
		if (null != paraDomain) {
			BeanUtils.copyProperties(paraDomain, bean);
			return true;
		} else {
			// saveErrorLoginUser(bean);
		}

		return false;
	}

	@Override
	public UserEntity getFullUserInfo(UserEntity user) throws Exception {
		//获取用户的模块列表
		List<ModuleEntity> tempList = moduleDao.getUserAllModule(user);
		user.setModulesList(tempList);
		//获取用户的角色
		RoleEntity roleEntity = new RoleEntity();
		roleEntity.setUser_id(user.getId());
		List<RoleEntity> roleList = roleDao.getUserRoleList(roleEntity);
		user.setRolesList(roleList);
		return user;
	}
}
